//
//  UIView+zoomScaleSize.m
//  TravelGuideMdd
//
//  Created by 陈曦 on 14/12/1.
//  Copyright (c) 2014年 mafengwo.com. All rights reserved.
//

#import "UIView+zoomScaleSize.h"

@implementation UIView (zoomScaleSize)

static const char sizeToFitHeightKey;
static const char sizeToFitWidthKey;

- (CGFloat)sizeToFitHeight
{
    return [[self getAssociativeValue:&sizeToFitHeightKey] floatValue];
}

- (void)setSizeToFitHeight:(CGFloat)sizeToFitHeight
{
    return [self setRetainNonatomicProperty:[NSNumber numberWithFloat:sizeToFitHeight]
                                      byKey:&sizeToFitHeightKey];
}

- (CGFloat)sizeToFitWidth
{
    return [[self getAssociativeValue:&sizeToFitWidthKey] floatValue];
}

- (void)setSizeToFitWidth:(CGFloat)sizeToFitWidth
{
    [self setRetainNonatomicProperty:[NSNumber numberWithFloat:sizeToFitWidth]
                               byKey:&sizeToFitWidthKey];
}



- (CGSize)theNewIntrinsicContentSize
{
    CGSize size = [self theNewIntrinsicContentSize];
    CGFloat width = self.sizeToFitWidth;
    CGFloat height = self.sizeToFitHeight;
    if (width > 0)
    {
        CGFloat scale = width / size.width;
        size.height = size.height * scale;
        size.width = width;
    }
    else if (height > 0)
    {
        CGFloat scale = height / size.height;
        size.width = size.width * scale;
        size.height = height;
    }
    return size;
}

+ (void)load
{
    Method m1 = class_getInstanceMethod(self, @selector(intrinsicContentSize));
    Method m2 = class_getInstanceMethod(self, @selector(theNewIntrinsicContentSize));
    method_exchangeImplementations(m1, m2);
}

@end
